package com.ccbooks.view;

import com.ccbooks.adapter.BookInfosAdapter;
import com.ccbooks.bo.BooksBo;
import com.ccbooks.bo.FileBo;
import com.ccbooks.dao.BooksDao;
import com.ccbooks.util.BookUtil;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class BookListView extends ListActivity implements OnScrollListener {

	// ��ÿؼ�
	private Button btnBookStore;
	private Button btnBookshelf;
	private Button btnBooklist;
	private Button btnEdit;
	private Button btnReflesh;

	BookInfosAdapter bia;
	private Dialog pd;
	// public List<ImageView> ivs;
	public boolean isEdit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bookslist);
		getListView().setScrollbarFadingEnabled(true);
		findview();
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		doing();
	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i("zkli", "shell finishing====");
		BooksDao.getDao(this).close();
	}

	private void doing() {
		showview();
		btnBooklist.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.listonclick));
		btnBookshelf.setOnClickListener(btnBookshelfOnclick);
		btnReflesh.setOnClickListener(btnRefleshOnclick);
		btnEdit.setOnClickListener(btnEditOnclick);
	}

	private void showview() {
		// String[] strs={"first","second"};
		BooksBo butil = new BooksBo(this, false);
		butil.firstBookList();
		// ivs=new ArrayList<ImageView>();
		this.bia = new BookInfosAdapter(this, BooksBo.books);
		// bia.count=4;
		// ListAdapter la=new
		// ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strs);
		setListAdapter(bia);
		getListView().setOnScrollListener(this);
		//new Thread(butil).start();
	}

	private void findview() {
		btnBookStore = (Button) findViewById(R.id.btnBookStore);
		btnBookshelf = (Button) findViewById(R.id.btnBookshelf);
		btnBooklist = (Button) findViewById(R.id.btnBooklist);
		btnEdit = (Button) findViewById(R.id.btnEdit);
		btnReflesh = (Button) findViewById(R.id.btnReflesh);
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		// TODO Auto-generated method stub
		if (BookUtil.bookSize(BooksBo.books) < BookUtil.getCount(this)) {
			// bia.count += visibleItemCount; // or any other amount
			bia.notifyDataSetChanged();
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub

	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putInt("bookId", BooksBo.books.get(position).id);
		intent.putExtras(bundle);
		intent.setClass(this, BookContentView.class);
		startActivity(intent);
		super.onListItemClick(l, v, position, id);
	}
	
	@Override
	public boolean onTrackballEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		return true;
	}

	private OnClickListener btnBookshelfOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Intent intent = new Intent();
			intent.setClass(BookListView.this, BookShelfView.class);
			startActivity(intent);
			finish();
		}

	};

	private OnClickListener btnRefleshOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			pd = new Dialog(BookListView.this);
			pd.requestWindowFeature(Window.FEATURE_NO_TITLE);
			pd.setContentView(R.layout.dia_loading);
			FileBo fileUtil = new FileBo(BookListView.this, "/sdcard");
			new Thread(fileUtil).start();
			pd.show();
		}

	};

	private OnClickListener btnEditOnclick = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			isEdit = !isEdit;
			bia = new BookInfosAdapter(BookListView.this, BooksBo.books);
			setListAdapter(bia);
			if (isEdit) {
				btnEdit.setText("完成");
				btnEdit.setBackgroundDrawable(BookListView.this.getResources()
						.getDrawable(R.drawable.onclickbutton));
			} else {
				btnEdit.setText("编辑");
				btnEdit.setBackgroundDrawable(BookListView.this.getResources()
						.getDrawable(R.drawable.outclickbutton));
			}
		}

	};
	
	//刷新Handler
	public Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			doing();
			if(pd != null){
				pd.cancel();
			}
			super.handleMessage(msg);
		}
		
	};
}
